//
//  LikeGiftViewController.m
//  礼物说
//
//  Created by dllo on 16/5/4.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "LikeGiftViewController.h"

@interface LikeGiftViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation LikeGiftViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc{
    [_array release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"喜欢的礼物";
    self.tabBarController.tabBar.hidden = YES;
    [self createTable];
}

#pragma mark - 创建TableView
- (void)createTable{
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 49) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[LikeGiftTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSLog(@"%ld", _array.count);
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LikeGiftTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = _array[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return kHEIGHTS(100);
}

#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LiWu * model = _array[indexPath.row];
    HotContentViewController * Vc = [[HotContentViewController alloc] init];
    Vc.judge = @"收藏";
    Vc.giftNumber = (NSNumber *)model.url;
    [self.navigationController pushViewController:Vc animated:YES];
    [Vc release];
}
// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // 首先执行父类的编辑按钮方法
    [super setEditing:editing animated:animated];
    // 设置tableview的编辑状态
    [_tableView setEditing:editing animated:animated];
}

// 实现编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 2. 从tableview中 按位置删除
        LiWu * liwu = [[[LiWu alloc] init] autorelease];
        liwu = _array[indexPath.row];
        [[DatabaseHandle shareDataBase] deleteDataWithLiWu:liwu Name:liwu.name];
        [_array removeObjectAtIndex:indexPath.row];
        [_tableView reloadData];
    }
}

// 修改删除按钮的文字 (默认是delete)
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消收藏";
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
